<template>
  <el-drawer
    v-model="drawerVisible"
    :destroy-on-close="true"
    @close="handleDrawerClose"
    size="450px"
    :title="`${drawerProps.title}菜单`"
  >
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="form"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="上级菜单">
        <el-select v-model="form.iShangJiRuleId" @change="changeShangJi">
          <el-option v-for="item in allmenus" :key="item" :label="item.Title" :value="item.RuleId" />
        </el-select>
      </el-form-item>
      <el-form-item label="规则类型" prop="strType">
        <el-radio-group v-model="form.strType" class="ml-4">
          <el-button v-if="form.iShangJiRuleId == ''"><el-radio label="菜单目录" size="large">菜单目录</el-radio></el-button>
          <el-button plain><el-radio label="菜单项" size="large">菜单项</el-radio></el-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="路由标题" prop="strTitle">
        <el-input v-model="form.strTitle"></el-input>
      </el-form-item>
      <el-form-item label="路由路径" prop="strPath">
        <el-input v-model="form.strPath"></el-input>
      </el-form-item>
      <el-form-item label="路由名称" prop="strName">
        <el-input v-model="form.strName"></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="strIcon">
        <SelectIcon v-model:icon-value="form.strIcon" />
      </el-form-item>
      <el-form-item label="重定向地址" prop="strRedirect">
        <el-input v-model="form.strRedirect"></el-input>
      </el-form-item>
      <el-form-item label="组件地址" prop="strComponent">
        <el-input v-model="form.strComponent"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="QuXiao">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive, onMounted } from "vue";
import { FormInstance, ElMessage } from "element-plus";
// import { User } from "@/api/interface";
import SelectIcon from "@/components/SelectIcon/index.vue";
import { GetCaiDanMuLuAllport } from "@/api/modules/user";

// PostCaiDanRuleAddport
const rules = reactive({
  strTitle: [{ required: true, message: "不可为空", trigger: "blur" }],
  strPath: [{ required: true, message: "不可为空", trigger: "blur" }],
  strName: [{ required: true, message: "不可为空", trigger: "blur" }],
  strComponent: [{ required: true, message: "不可为空", trigger: "blur" }],
  // strRedirect: [{ required: true, message: "不可为空", trigger: "blur" }],
  strType: [{ required: true, message: "不可为空", trigger: "blur" }],
  strIcon: [{ required: true, message: "不可为空", trigger: "blur" }]
});
// 储存所有的菜单目录
const allmenus = ref<any>([]);

const handleDrawerClose = () => {
  clear();
};

const changeShangJi = () => {
  form.strType = "";
};

const clear = () => {
  form.iRuleId = 0;
  form.iShangJiRuleId = "";
  form.strType = "";
  form.strTitle = "";
  form.strPath = "";
  form.strName = "";
  form.strIcon = "";
  form.strRedirect = "";
  form.strComponent = "";
  form.isLink = false;
  form.isHide = false;
  form.isFull = false;
  form.isAffix = false;
  form.isKeepAlive = true;
  drawerVisible.value = false;
};
const QuXiao = () => {
  drawerVisible.value = false;
  clear();
};

const form = reactive<any>({
  iRuleId: 0,
  iShangJiRuleId: "",
  strType: "",
  strTitle: "",
  strPath: "",
  strName: "",
  strIcon: "",
  strRedirect: "",
  strComponent: "",
  isLink: false,
  isHide: false,
  isFull: false,
  isAffix: false,
  isKeepAlive: true
});

const drawerVisible = ref(false);
const drawerProps = ref<any>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: any) => {
  console.log(params, "paramsparamsparamsparams");
  drawerProps.value = params;
  console.log(drawerProps.value, "drawerProps.valuedrawerProps.valuedrawerProps.valuedrawerProps.value");
  if (Object.entries(drawerProps.value.row).length !== 0) {
    form.iRuleId = drawerProps.value.row.iRuleId;
    form.iShangJiRuleId = drawerProps.value.row.iShangJiRuleId;
    form.strType = drawerProps.value.row.strType;
    form.strTitle = drawerProps.value.row.meta.title;
    form.strPath = drawerProps.value.row.path;
    form.strName = drawerProps.value.row.name;
    form.strIcon = drawerProps.value.row.meta.icon;
    form.strRedirect = drawerProps.value.row.redirect ? drawerProps.value.row.redirect : "";
    form.strComponent = drawerProps.value.row.component;
    form.isLink = false;
    form.isHide = false;
    form.isFull = false;
    form.isAffix = false;
    form.isKeepAlive = true;
  }
  drawerVisible.value = true;
};

const immediately = () => {
  GetCaiDanMuLuAllport().then((res: any) => {
    allmenus.value = res.Data;
    console.log(allmenus.value);
  });
};

const emit = defineEmits(["mnenuset"]) as any;

onMounted(() => {
  immediately();
});

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  if (form.strType == "菜单目录") {
    rules.strComponent[0].required = false;
  }
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      // await drawerProps.value.api!(form);

      if (form.strType == "菜单项" && form.iShangJiRuleId == "") {
        return ElMessage({
          showClose: true,
          message: "请选择上级菜单",
          type: "error"
        });
      }
      if (form.iShangJiRuleId == "") {
        form.iShangJiRuleId = 0;
      }
      drawerProps.value.api!(form).then((res: any) => {
        console.log(res);
        if (res.Success == 0) {
          ElMessage.success({ message: "设置成功" });
          emit("mnenuset", "111");
        } else {
          ElMessage({
            showClose: true,
            message: res.Message,
            type: "error"
          });
        }
      });
      // drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams,
  immediately
});
</script>
